package com.donghuayuan.kotlincar.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.donghuayuan.kotlincar.BrandListActivity
import com.donghuayuan.kotlincar.R
import com.donghuayuan.kotlincar.bean.Result
import com.donghuayuan.kotlincar.databinding.ItemLogoBinding

class LogoAdapter(var context: Context, var mList: MutableList<Result>) :
    RecyclerView.Adapter<LogoAdapter.LogoHolder>() {

    val inflater = LayoutInflater.from(context)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogoHolder {
        val binding: ItemLogoBinding =
            DataBindingUtil.inflate(inflater, R.layout.item_logo, parent, false)
        val holder = LogoHolder(binding.root)
        holder.binding = binding
        return holder
    }

    override fun onBindViewHolder(holder: LogoHolder, position: Int) {
        val bean = mList[position]
        holder.binding?.item = bean

        holder.binding?.root?.setOnClickListener {
            val intent = Intent(context, BrandListActivity::class.java)
            intent.putExtra("bean",bean.id)
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    class LogoHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding: ItemLogoBinding? = null

    }

    open fun addDatas(list: MutableList<Result>?) {
        if (!list.isNullOrEmpty()) {
            mList.clear()
            mList.addAll(list)
            this@LogoAdapter.notifyDataSetChanged()
        }
    }
}